package com.itheima.mp.time;

import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.ZonedDateTimeSerializer;
import com.itheima.mp.domain.po.JosnTest;
import lombok.val;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.TimeZone;

@SpringBootTest
public class JsonTimeExample {
    @Test
    public void testtimetype() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        // 注册 Java Time Module
        // 注册 Java Time Module
        JavaTimeModule module = new JavaTimeModule();
        // 设置时区
        objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        objectMapper.setLocale(Locale.CHINA);

        // 自定义 ZonedDateTime 的序列化和反序列化格式
        module.addSerializer(ZonedDateTime.class, new ZonedDateTimeSerializer(DateTimeFormatter.ISO_OFFSET_DATE_TIME));

        objectMapper.registerModule(module);
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); // 禁止将日期以时间戳的形式序列化

        // 示例 Java 时间对象
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        ZonedDateTime zonedDateTime = ZonedDateTime.now();

        // 序列化 Java 时间对象为 JSON 字符串
        String localDateJson = objectMapper.writeValueAsString(localDate);
        String localTimeJson = objectMapper.writeValueAsString(localTime);
        String localDateTimeJson = objectMapper.writeValueAsString(localDateTime);
        String zonedDateTimeJson = objectMapper.writeValueAsString(zonedDateTime);

        // 打印转换后的 JSON 字符串
        System.out.println("LocalDate: " + localDateJson);
        System.out.println("LocalTime: " + localTimeJson);
        System.out.println("LocalDateTime: " + localDateTimeJson);
        System.out.println("ZonedDateTime: " + zonedDateTimeJson);

        // 将 JSON 字符串转换回 Java 时间类型
        LocalDate localDateFromJson = objectMapper.readValue(localDateJson, LocalDate.class);
        LocalTime localTimeFromJson = objectMapper.readValue(localTimeJson, LocalTime.class);
        LocalDateTime localDateTimeFromJson = objectMapper.readValue(localDateTimeJson, LocalDateTime.class);
        ZonedDateTime zonedDateTimeFromJson = objectMapper.readValue(zonedDateTimeJson, ZonedDateTime.class);

        // 打印转换回的 Java 时间对象
        System.out.println("LocalDate from JSON: " + localDateFromJson);
        System.out.println("LocalTime from JSON: " + localTimeFromJson);
        System.out.println("LocalDateTime from JSON: " + localDateTimeFromJson);
        System.out.println("ZonedDateTime from JSON: " + zonedDateTimeFromJson);


        // 使用 API 操作这些时间对象
        System.out.println("\nLocalDate Operations:");
        System.out.println("Year: " + localDateFromJson.getYear());
        System.out.println("Month: " + localDateFromJson.getMonthValue());
        System.out.println("Day: " + localDateFromJson.getDayOfMonth());
        System.out.println("Next Week: " + localDateFromJson.plusDays(7));
        System.out.println("Last Month: " + localDateFromJson.minusMonths(1));
        System.out.println("Is before today: " + localDateFromJson.isBefore(LocalDate.now()));
        System.out.println("Is after today: " + localDateFromJson.isAfter(LocalDate.now()));

        System.out.println("\nLocalTime Operations:");
        System.out.println("Hour: " + localTimeFromJson.getHour());
        System.out.println("Minute: " + localTimeFromJson.getMinute());
        System.out.println("Second: " + localTimeFromJson.getSecond());
        System.out.println("One Hour Later: " + localTimeFromJson.plusHours(1));
        System.out.println("Ten Minutes Ago: " + localTimeFromJson.minusMinutes(10));
        System.out.println("Is before noon: " + localTimeFromJson.isBefore(LocalTime.NOON));
        System.out.println("Is after midnight: " + localTimeFromJson.isAfter(LocalTime.MIDNIGHT));

        System.out.println("\nLocalDateTime Operations:");
        System.out.println("Year: " + localDateTimeFromJson.getYear());
        System.out.println("Month: " + localDateTimeFromJson.getMonthValue());
        System.out.println("Day: " + localDateTimeFromJson.getDayOfMonth());
        System.out.println("Hour: " + localDateTimeFromJson.getHour());
        System.out.println("Minute: " + localDateTimeFromJson.getMinute());
        System.out.println("Tomorrow Same Time: " + localDateTimeFromJson.plusDays(1));
        System.out.println("One Week Earlier: " + localDateTimeFromJson.minusWeeks(1));
        System.out.println("Only Date: " + localDateTimeFromJson.toLocalDate());
        System.out.println("Only Time: " + localDateTimeFromJson.toLocalTime());

        System.out.println("\nZonedDateTime Operations:");
        System.out.println("Zone: " + zonedDateTimeFromJson.getZone());
        System.out.println("Offset: " + zonedDateTimeFromJson.getOffset());
        System.out.println("Date: " + zonedDateTimeFromJson.toLocalDate());
        System.out.println("Time: " + zonedDateTimeFromJson.toLocalTime());
        System.out.println("UTC Time: " + zonedDateTimeFromJson.withZoneSameInstant(ZoneId.of("UTC")));
        System.out.println("Is before now: " + zonedDateTimeFromJson.isBefore(ZonedDateTime.now()));
        System.out.println("Is after now: " + zonedDateTimeFromJson.isAfter(ZonedDateTime.now()));
        System.out.println("LocalDateTime: " + zonedDateTimeFromJson.toLocalDateTime());


    }

    @Test
    public void testtimevalue() throws JsonProcessingException {
        // 示例 Java 时间对象
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        ZonedDateTime zonedDateTime = ZonedDateTime.now();

        // 使用 API 操作这些时间对象
        System.out.println("\nLocalDate Operations:");
        System.out.println("Year: " + localDate.getYear());
        System.out.println("Month: " + localDate.getMonthValue());
        System.out.println("Day: " + localDate.getDayOfMonth());
        System.out.println("Next Week: " + localDate.plusDays(7));
        System.out.println("Last Month: " + localDate.minusMonths(1));
        System.out.println("Is before today: " + localDate.isBefore(LocalDate.now()));
        System.out.println("Is after today: " + localDate.isAfter(LocalDate.now()));

        System.out.println("\nLocalTime Operations:");
        System.out.println("Hour: " + localTime.getHour());
        System.out.println("Minute: " + localTime.getMinute());
        System.out.println("Second: " + localTime.getSecond());
        System.out.println("One Hour Later: " + localTime.plusHours(1));
        System.out.println("Ten Minutes Ago: " + localTime.minusMinutes(10));
        System.out.println("Is before noon: " + localTime.isBefore(LocalTime.NOON));
        System.out.println("Is after midnight: " + localTime.isAfter(LocalTime.MIDNIGHT));

        System.out.println("\nLocalDateTime Operations:");
        System.out.println("Year: " + localDateTime.getYear());
        System.out.println("Month: " + localDateTime.getMonthValue());
        System.out.println("Day: " + localDateTime.getDayOfMonth());
        System.out.println("Hour: " + localDateTime.getHour());
        System.out.println("Minute: " + localDateTime.getMinute());
        System.out.println("Tomorrow Same Time: " + localDateTime.plusDays(1));
        System.out.println("One Week Earlier: " + localDateTime.minusWeeks(1));
        System.out.println("Only Date: " + localDateTime.toLocalDate());
        System.out.println("Only Time: " + localDateTime.toLocalTime());

        System.out.println("\nZonedDateTime Operations:");
        System.out.println("Zone: " + zonedDateTime.getZone());
        System.out.println("Offset: " + zonedDateTime.getOffset());
        System.out.println("Date: " + zonedDateTime.toLocalDate());
        System.out.println("Time: " + zonedDateTime.toLocalTime());
        System.out.println("UTC Time: " + zonedDateTime.withZoneSameInstant(ZoneId.of("UTC")));
        System.out.println("Is before now: " + zonedDateTime.isBefore(ZonedDateTime.now()));
        System.out.println("Is after now: " + zonedDateTime.isAfter(ZonedDateTime.now()));
        System.out.println("LocalDateTime: " + zonedDateTime.toLocalDateTime());
    }
    @Test
    public void testtimepojo() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        // 注册 Java Time Module
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
        javaTimeModule.addSerializer(ZonedDateTime.class, new ZonedDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        objectMapper.registerModule(javaTimeModule);

        // 创建 JosnTest 对象
        JosnTest josnTest = new JosnTest();
        josnTest.setId("1");
        josnTest.setJosnData("data");
        josnTest.setCreateTime(LocalDateTime.now());
        josnTest.setUpdateTime(LocalDateTime.now());
        josnTest.setZonetime(ZonedDateTime.now());

        // 序列化 JosnTest 对象为 JSON 字符串
        String json = objectMapper.writeValueAsString(josnTest);
        System.out.println("JSON String: " + json);

        // 将 JSON 字符串转换回 JosnTest 对象
        JosnTest josnTestFromJson = objectMapper.readValue(json, JosnTest.class);
        System.out.println("JosnTest from JSON: " + josnTestFromJson);
    }


//找了半天终于还是hutool的最好用妈的
    @Test
    public void testhutooltime() throws JsonProcessingException {

        // 创建 JosnTest 对象
        JosnTest josnTest = new JosnTest();
        josnTest.setId("1");
        josnTest.setJosnData("data");
        josnTest.setCreateTime(LocalDateTime.now());
        josnTest.setUpdateTime(LocalDateTime.now());
        josnTest.setZonetime(ZonedDateTime.now());
        String hutoolstr = JSONUtil.toJsonStr(josnTest);
        String jsonPrettyStr = JSONUtil.toJsonPrettyStr(josnTest);
        System.out.println("JSON Pretty String: " + jsonPrettyStr);

        JosnTest bean = JSONUtil.toBean(hutoolstr, JosnTest.class);
        System.out.println("Bean: " + bean);


    }


}
